package com.zhyt.intelligentpipeline.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhyt.intelligentpipeline.model.CorrosionDetection;
import com.zhyt.intelligentpipeline.model.DeformationDetection;
import com.zhyt.intelligentpipeline.service.DeformationDetectionService;
import com.zhyt.intelligentpipeline.vo.PageVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/DeformationDetection")
public class DeformationDetectionController {
    @Resource
    private DeformationDetectionService deformationDetectionService;
    @GetMapping
    public List<DeformationDetection>getAll(){
        return deformationDetectionService.list();
    }
    //分页
    @GetMapping("page/{page}/num/{num}")
    public IPage<DeformationDetection> getPage(@PathVariable Integer page, @PathVariable Integer num)
    {
        IPage<DeformationDetection>pages=new Page<>(page,num);
        return deformationDetectionService.page(pages);
    }

        @GetMapping("pipeline/{id}")
    public List<DeformationDetection> getCorrosionDetectionsByPipelineId(@PathVariable(value = "id")String pipelineId){
        return deformationDetectionService.getDeformationDetectionByPipelineID(pipelineId);
    }
    @GetMapping("pipeline/{id}/page/{page}/num/{num}")
    public List<DeformationDetection> getCorrosionDetectionsByPipelineIdPage(@PathVariable(value = "id")String pipelineId,
                                                                           @PathVariable(value = "page") Integer page,
                                                                           @PathVariable(value = "num") Integer num ){
        List<DeformationDetection> corrosionDetections = deformationDetectionService.getDeformationDetectionByPipelineID(pipelineId);
        int endValue = Math.min(page * num, corrosionDetections.size());
        return corrosionDetections.subList((page - 1) * num ,endValue);
    }

    @GetMapping("pipeline/{id}/pageInfo/num/{num}")
    public PageVo getPageInfo(@PathVariable(value = "id")String pipelineId,
                              @PathVariable(value = "num")Integer num){
        int counts = deformationDetectionService.getDeformationDetectionByCount(pipelineId);
        PageVo pageVo = new PageVo();
        pageVo.setAllValues(counts);
        pageVo.setEveryPageNums(num);
        pageVo.setPages((int)Math.ceil(counts / (double) num));
        return pageVo;
    }
    //修改
    @PutMapping     //是返回的Boolea类型
    public Boolean update(@RequestBody DeformationDetection deformationDetection)
    {
        return deformationDetectionService.updateById(deformationDetection);
    }
    /**
     * 删除
     */
    @DeleteMapping("delete/{id}")//这里面的id是和形参中的id是相应的才能传过去
    public Boolean delete(@PathVariable String id)
    {
        return deformationDetectionService.removeById(id);
    }
}
